<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!doctype html>
<html>
	<head>
		<base href="<%=basePath%>" />
		<title>登陆 - </title>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta name="renderer" content="webkit">
		<meta http-equiv="Cache-Control" content="no-siteapp" />
		<link rel="icon" type="image/png" href="i/buyu.ico">
		<link rel="apple-touch-icon-precomposed" href="i/app-icon72x72@2x.png">
		<meta name="apple-mobile-web-app-title" content="Amaze UI" />
		<link rel="stylesheet" href="css/amazeui.min.css" />
		<link rel="stylesheet" href="css/admin.css">
		<link rel="stylesheet" href="css/app.css">
		<link rel="stylesheet" href="css/fonts.css">
		<style type="text/css">
			.am-icon-exclamation-triangle:before,
			.am-icon-warning:before {
				content: ''
			}

			.am-form-horizontal .am-form-feedback [class*=am-icon-] {
				right: 0.6em;
			}

			.am-form-horizontal .am-form-feedback [class*=am-icon-] {
				top: 30%;
			}

			.am-icon-warning img {
				cursor: pointer;
			}
		</style>
	</head>

	<body data-type="login">

		<div class="am-g myapp-login">
			<div class="myapp-login-logo-block  tpl-login-max">
				<div class="myapp-login-logo-text">
					<div class="myapp-login-logo-text">
						<span> 后台登陆</span> <i class="iconfont">&#xe71b;</i>
					</div>
				</div>

				<div class="login-font">
					<i> </i>
				</div>
				<div class="am-u-sm-10 login-am-center">
					<form class="am-form am-form-horizontal" id="loginForm">
						<div class="am-form-group am-form-success am-form-icon am-form-feedback">
							<input type="text" class="am-form-field am-radius" placeholder="账号" id="loginId" autocomplete="off">
						</div>

						<div class="am-form-group am-form-success am-form-icon am-form-feedback">
							<input type="password" class="am-form-field am-radius" placeholder="密码" id="pwd" autocomplete="off">
						</div>


						<div class="am-form-group am-form-success am-form-icon am-form-feedback">
							<input type="text" class="am-form-field am-radius" placeholder="验证码" id="valiCode" maxlength="4" autocomplete="off">
							<span class="am-icon-warning">
								<img src="imageCode" id="imgcode">
							</span>
						</div>

						<div class="am-form-group am-form-success am-form-icon am-form-feedback">
							<button type="submit" class="am-btn am-btn-default">登录</button>
						</div>



					</form>
				</div>
			</div>
		</div>

		<script src="js/jquery.min.js"></script>
		<script src="js/layer${applicationScope.skin}/layer.js"></script>
		<script src="js/rsa.js"></script>
		<script>
		    var public_key ;
		    $(document).ready(function(){
		    	//定义公钥
		    	//获取公钥
	    		$.ajax({
	                url:"publickey",
	                type:"post",
	                success:function(res){
	                	res = JSON.parse(res);
	                    if(res.code === 200){
	                    	public_key = res.public_key;
	                    }
	                }
	            })
		    });
			if (window.parent.length > 0) {
				window.parent.location = "login";
			}
			$(function() {
				$("#imgcode").click(function() {
					$(this).hide().attr("src", "imageCode?s=" + Math.random()).fadeIn();
					$("#valiCode").val("").focus();
				});
				$("#loginForm").submit(function() {
					var encrypt=new JSEncrypt();
			    	encrypt.setPublicKey(public_key);
			    	
					var loginId = $("#loginId").val();
					var pwd = encrypt.encrypt($("#pwd").val());
					var valiCode = $("#valiCode").val();
                    console.log(pwd);
					if (isEmpty("loginId", "请输入账号")) {
						return false;
					}
					if (isEmpty("pwd", "请输入密码")) {
						return false;
					}
					// if (isEmpty("valiCode", "请输入验证码")) {
					// 	return false;
					// }
					var loading = layer.msg('正在登陆..', {
						shade: 0.00001,
						icon: 16,
						time: 1000 * 60 * 10
					});
					$.ajax({
						type: "POST",
						url: "loginDo?loginId=" + loginId + "&pwd=" + pwd + "&valiCode=" + valiCode,
						dataType: "json",
						error: function() {
							layer.close(loading);
							layer.msg("登陆异常，请稍后重试！");
						},
						success: function(data) {
							layer.close(loading);
							if (0 == data.code) {
								location.href = "<%=basePath%>";
							} else {
								layer.msg(data.msg, {
									offset: ['30%']
								});
								if (3 <= data.code && data.code <= 5) {
									$("#valiCode").val("").focus();
									$("#imgcode").attr("src", "imageCode?s=" + Math.random()).fadeIn();
								}
							}
						}
					});
					return false;

				});

				var tips = function(msg, id) {
					layer.tips(msg, '#' + id, {
						tips: [1, '#ec4758'] //还可配置颜色
					});
				};

				var isEmpty = function(id, msg) {
					var input = $("#" + id);
					if (null == input.val() || "" == input.val() || "" == $.trim(input.val())) {
						tips(msg, id);
						input.focus();
						return true;
					}
					return false;
				}
			});
		</script>
	</body>

</html>
